package org.visitor.visitorserver.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.visitor.visitorserver.entity.Professor;
import org.visitor.visitorserver.entity.User;
import org.visitor.visitorserver.mapper.UserMapper;
import org.visitor.visitorserver.service.UserServiceInter;

import java.sql.Date;
import java.util.Map;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserServiceInter {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void insertOneUser(Map<String, String> request) {
        User user = new User();
        user.setId(UUID.randomUUID().toString());

        String username = request.get("userName");
        if (username == null) {
            throw new RuntimeException("用户名不能为空");
        }
        user.setUsername(username);

        // todo: 密码加密加salt
        String pwd = request.get("pwd");
        if (pwd == null) {
            throw new RuntimeException("密码不能为空");
        }
        if (pwd.length() < 8) {
            throw new RuntimeException("密码小于8位");
        }
        user.setPwd(pwd);


        String tel = request.get("tel");
        if (tel == null) {
            throw new RuntimeException("电话号码不能为空");
        }
        if (tel.length() != 11) {
            throw new RuntimeException("电话号码不满11位");
        }
        user.setTel(tel);


        String userSfz = request.get("userSfz");
        if (userSfz == null) {
            throw new RuntimeException("身份证不能为空");
        }
        if (userSfz.length() != 18) {
            throw new RuntimeException("身份证不符合要求");
        }
        user.setUserSfz(userSfz);


        String userCompany = request.get("userCompany");
        if (userCompany == null) {
            throw new RuntimeException("单位不能为空");
        }
        user.setUserCompany(userCompany);

        String zw = request.get("zw");
        if (zw == null) {
            throw new RuntimeException("职务不能为空");
        }
        user.setZw(zw);

        Date date = new Date(new java.util.Date().getTime());
        user.setCreateAt(date);
        user.setUpdateAt(date);

        user.setIsDel(0);

        int rows = userMapper.insert(user);
        if (rows <= 0) {
            throw new RuntimeException("数据插入失败");
        }

    }

    @Override
    public void updateOneUser(Map<String, String> requestBody) {
        User user = new User();
        String id = requestBody.get("id");
        if (id == null || id.isEmpty()) {
            throw new RuntimeException("未知的要更新的专家");
        }
        user.setId(id);

        String newUserName = requestBody.get("name");
        if (newUserName != null && !newUserName.isEmpty()) {
            user.setUsername(newUserName);
        }

        String newPwd = requestBody.get("area");
        if (newPwd != null && !newPwd.isEmpty()) {
            user.setPwd(newPwd);
        }

        String newTel = requestBody.get("tel");
        if (newTel != null && !newTel.isEmpty()) {
            user.setTel(newTel);
        }

        String newUserSfz = requestBody.get("isCheck");
        if (newUserSfz != null && !newUserSfz.isEmpty()) {
            user.setUserSfz(newUserSfz);
        }

        String newUserCompany = requestBody.get("status");
        if (newUserCompany != null && !newUserCompany.isEmpty()) {
            user.setUserCompany(newUserCompany);
        }

        String newZw = requestBody.get("zw");
        if (newZw != null && !newZw.isEmpty()) {
            user.setZw(newZw);
        }


        Date date = new Date(new java.util.Date().getTime());
        user.setUpdateAt(date);

        int rows = userMapper.updateById(user);
        if (rows <= 0) {
            throw new RuntimeException("专家更新失败");
        }
    }

}
